// pages/particulars/particulars.js
import { particulars,joinShop,fav,favAdd,favDelete } from "../../utils/http";

Page({
     
  /**
   * 页面的初始数据
   */
  data: {
        carId:"",
        goodList:[],
        show:false,
        num:1,
        likes:false,
        sho:false,
        img:''
  },
  //添加商品收藏登录判断

  getToken(){
    let token=wx.getStorageSync('token');
    if(token){
      this.setData({
        sho:false
      })
    }else{
      this.setData({
        sho:true
      })
    }
  },

  //添加收藏
  joinLikes(){
    this.getToken()
    let token=wx.getStorageSync('token');
    //先通过判断他是收藏还是不收藏
    if(this.data.likes==false){
      favAdd({goodsId:this.data.goodList.basicInfo.id,token:token}).then(re=>{
        console.log(re)
        if(re.code==0){
          this.setData({
            likes:true
          })
          wx.showToast({
            title: '已收藏',
            icon: 'success',
            duration: 2000
        })
        }
      }).catch(er=>{
        console.log(er)
      })  

    }else{
      favDelete({goodsId:this.data.goodList.basicInfo.id,number:this.data.num,token:token})
      .then(rm=>{
        console.log(rm)
        if(rm.code==0){
          this.setData({
            likes:false
          })
          wx.showToast({
            title: '已取消收藏',
            icon: 'success',
            duration: 2000
        })
        }
      }).catch(ea=>{
        console.log(ea)
      })
    }
  },
  //加入购物车
  JoinShop(){
    this.getToken()
    this.setData({
      show:false
    })
    let token=wx.getStorageSync('token');
    
    console.log(this.data.num)
    console.log(token)
    joinShop({goodsId:this.data.goodList.basicInfo.id,number:this.data.num,token:token}).then(res=>{
       // console.log(res)
        if(res.code==0){
             wx.showToast({
            title: '加入购车成功',
            icon: 'success',
            duration: 2000
        })
        }
    }).catch(err=>{
      console.log(err)
    })
  },
  onChange(event) {
   
    this.setData({
      num:event.detail
    })
    //打印详情页添加的数量
   // console.log(event.detail);
  },
  //关闭弹出层
  GuanBi(){

    this.setData({
      show:false
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  //加入购物车
  joinList(){
   

    
    this.setData({
      show:true
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //传来商品id赋值
     // console.log(options)
      this.setData({
        carId:options.card
      })
      //详情页数据
      particulars({id:this.data.carId}).then(xq=>{
        this.setData({
          goodList:xq.data,
          img:xq.data.content.replace(/<img/g,"<img class='richImg'")
        })
        //初次进入页面要检测是否已收藏
        let token=wx.getStorageSync('token');
             // console.log(this.data.goodList.basicInfo.id)
         if(token){
          fav({goodsId:this.data.goodList.basicInfo.id,token:token}).then(re=>{
            console.log(re)
            if(re.code==-1){
              this.setData({
                likes:false
              })
             // console.log(this.data.likes)
            }else{
              this.setData({
                likes:true
              })
            }
          }).catch(er=>{
            console.log(er)
          }) 
         }else{
          this.setData({
            likes:false
          })
         }
             

      }).catch(xqq=>{
        console.log(xqq)
      })
      
    


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})